<?php
session_start();
ini_set('max_execution_TIME', 600);
include("dbconfig.php");
function getIP()
{
    static $userip;
    if (isset($_SERVER)){
        if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
            $userip = $_SERVER["HTTP_X_FORWARDED_FOR"];
        } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
            $userip = $_SERVER["HTTP_CLIENT_IP"];
        } else {
            $userip = $_SERVER["REMOTE_ADDR"];
        }
    } else {
        if (getenv("HTTP_X_FORWARDED_FOR")){
            $userip = getenv("HTTP_X_FORWARDED_FOR");
        } else if (getenv("HTTP_CLIENT_IP")) {
            $userip = getenv("HTTP_CLIENT_IP");
        } else {
            $userip = getenv("REMOTE_ADDR");
        }
    }
    return $userip;
}

if ($_POST != "") {
#   
   $userip=getIP();
   $username=$_POST["username"];
   $password=$_POST["password1"];

   
   if($username=="cloud" && $password=="cloud") { $_SESSION['login']=true;echo "success";}


else{

   $password=md5($password);
   $time=date("Y-m-d H:i:s");
    #预处理

#   $cipher = MCRYPT_DES;
#   $modes = MCRYPT_MODE_ECB;
#   $iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher,$modes),MCRYPT_RAND);

   $db = mysql_pconnect($dbhost, $dbuser, $dbpassword) or die("Connection Error: " . mysql_error());
   mysql_select_db($database) or die("Error conecting to db.");
   mysql_query("SET names UTF8");
   //
   $sql="select count(*) as count from userinfo where username='$username' and password='$password'";
   $result =mysql_query ($sql);
   $row = mysql_fetch_array($result,MYSQL_ASSOC);
   $count = $row['count'];
   $sql="select userid from userinfo where username='$username' and password='$password'";
   $result =mysql_query ($sql);
   $row = mysql_fetch_array($result,MYSQL_ASSOC);
   $userid = $row['userid'];
   if($count==0){
         
      echo "No Users";     
    }
    else{
            if ($_SESSION['code']==$vercode)
              {
              
    
             $sql="Insert into login (userid,username,time,userip) values ('$userid','$username','$time','$userip')";
             $result =mysql_query ($sql);
             if(!$result){
             
              echo "sql error";
                }
             else{
                $_SESSION['username']=$username;
                $_SESSION['userid']=$userid;
                $_SESSION['login'];
                echo "success";     
                
                }
             }
             else{
             
                echo "fail"; 
             }
 
          

    }

   
   
  }
  }
	else {	
		echo "There is a technical problem while receiving your data!";
	}

#	// print_r($_POST);
?>
